package com.shopx.cm.service.impl;


import com.shopx.base.constants.ResultCodeEnum;
import com.shopx.base.exception.ShopxException;
import com.shopx.base.service.ISysSeqService;
import com.shopx.cm.bean.CmMeasure;
import com.shopx.cm.dao.db1.CmMeasureDao;
import com.shopx.cm.dao.db2.MeasureDao;
import com.shopx.cm.dto.MaterialUserDTO;
import com.shopx.cm.dto.MeasureReqDTO;
import com.shopx.cm.model.CmMeasureModel;
import com.shopx.cm.service.ICmMeasureService;
import com.shopx.cmm.DTO.PageDTO;
import com.shopx.core.utils.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

/**
 * TODO
 *
 * @author lituo
 * @className: CmMeasure
 * @date 2025-01-07
 */
@Service
@Slf4j
public class CmMeasureServiceImpl implements ICmMeasureService {

    @Resource
    private CmMeasureDao cmMeasureDao;

    @Resource
    private MeasureDao measureDao;

    @Override
    public CmMeasureModel selectByPrimaryKey(CmMeasure cmMeasure) {
        if (cmMeasure == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        return cmMeasureDao.selectByPrimaryKey(cmMeasure);
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int insertSelective(CmMeasure cmMeasure) {
        if (cmMeasure == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = cmMeasureDao.insertSelective(cmMeasure);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int updateByPrimaryKeySelective(CmMeasure cmMeasure) {
        if (cmMeasure == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = cmMeasureDao.updateByPrimaryKeySelective(cmMeasure);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    @Transactional(value = "db1", rollbackFor = Exception.class)
    public int deleteByPrimaryKey(CmMeasure cmMeasure) {
        if (cmMeasure == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        int result = cmMeasureDao.deleteByPrimaryKey(cmMeasure);
        if (result != 1) {
            throw new ShopxException(ResultCodeEnum.DB_ERROR);
        }
        return result;
    }

    @Override
    public int selectCount(CmMeasureModel cmMeasure) {
        return cmMeasureDao.selectCount(cmMeasure);
    }

    @Override
    public List<CmMeasureModel> selectList(CmMeasureModel cmMeasure) {
        List<CmMeasureModel> list = cmMeasureDao.selectList(cmMeasure);
        return list;
    }

    @Override
    public PageDTO<CmMeasureModel> getListPage(CmMeasureModel cmMeasure) {
        if (cmMeasure == null) {
            throw new ShopxException(ResultCodeEnum.PAR_NULL);
        }
        PageDTO<CmMeasureModel> pageDTO = new PageDTO<CmMeasureModel>();
        pageDTO.setiTotalDisplayRecords(selectCount(cmMeasure));
        pageDTO.setData(selectList(cmMeasure));
        return pageDTO;
    }

    /**
     * 同步erp数据
     */
    public void syncErpData(){
        log.info("CmMeasureService.syncErpDate.begin");MeasureReqDTO dto = new MeasureReqDTO();
        //查询当前天数之前一年的数据
        String endDt = DateTimeUtil.getDate8();
        endDt = DateTimeUtil.formatDtm(endDt+"000000");
        String begDt = DateTimeUtil.dateCalc(endDt,null,"D",-30);
        begDt = DateTimeUtil.formatDtm(begDt+"000000");
        dto.setBegDt(begDt);
        dto.setEndDt(endDt);
        List<CmMeasure> list = measureDao.queryList(dto);
        log.info("list.size:{}，准备同步数据",list.size());
        cmMeasureDao.truncateTable();
        log.info("清空数据完成");
        //2000条一次同步
        for (int i = 0; i < list.size(); i += 200) {
            List<CmMeasure> subList = list.subList(i, Math.min(i + 200, list.size()));
            log.info("分批同步开始同步，同步总数：{}",subList.size());
            cmMeasureDao.insertBatch(subList);
            log.info("分批同步数据完成，同步总数：{}",subList.size());
        }
        log.info("同步全部数据完成");
    }



//    public static void main(String[] args) {
//        List<CmMeasure> list = new ArrayList<>();
//        for(int i=0;i<98;i++){
//            CmMeasure cm = new CmMeasure();
//            cm.setCmmId(Long.valueOf(i));
//            list.add(cm);
//        }
//        for (int i = 0; i < list.size(); i += 20) {
//            List<CmMeasure> subList = list.subList(i, Math.min(i + 20, list.size()));
//            for(CmMeasure record:subList){
//                System.out.println(record.getCmmId());
//            }
//            System.out.println("****************");
//        }
//    }

}

